package com.tomato.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;

import com.google.appengine.api.datastore.Blob;
import com.tomato.bean.Ebook;
import com.tomato.dao.EbookDao;

@SuppressWarnings("serial")
public class EbookServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		Long id = null;
		// display ebook_image
		if (method.equals("show")) {
			id = Long.parseLong(req.getParameter("id"));
			if (id == null) {
				return;
			}
			Ebook ebook = EbookDao.getInstance().getById(id);
			if (ebook == null) {
				return;
			}
			Blob b = ebook.getImage();
			resp.setContentType("image/jpeg;charset=utf-8");
			resp.getOutputStream().write(b.getBytes());
			resp.getOutputStream().close();
		}
		// ebook content xml view
		if (method.equals("xml")) {
			resp.setContentType("text/xml;charset=utf-8");
			// all content xml list
			resp.sendRedirect("ebookAllList.jsp");
			// select content xml list
			//resp.sendRedirect("ebookSelectList.jsp");
			
		}
		// delete ebook
		if (method.equals("delete")) {
			id = Long.parseLong(req.getParameter("id"));
			EbookDao.getInstance().deleteById(id);
			resp.sendRedirect("index");
		}
		// ebook_image edit
		if (method.equals("edit")) {
			id = Long.parseLong(req.getParameter("id"));
			if (id == 0 || id == null) {
				resp.sendRedirect("index");
			}
			if (req.getMethod().equals("GET")) {
				Ebook ebook = null;
				ebook = EbookDao.getInstance().getById(id);
				req.setAttribute("ebook", ebook);
				try {
					req.getRequestDispatcher("ebookEdit.jsp")
							.forward(req, resp);
				} catch (ServletException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		// upload ebook and save in datastore
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		String author = null;
		String description = null;
		String genre = null;
		String title = null;
		Blob bEbook = null;
		if (method.equals("upload")) {
			ServletFileUpload upload = new ServletFileUpload();
			FileItemIterator iterator = null;
			try {
				iterator = upload.getItemIterator(req);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			try {
				while (iterator.hasNext()) {
					FileItemStream item = iterator.next();
					InputStream stream = item.openStream();
					if (item.isFormField()) {
						// Handle the uploaded file
						if (item.getFieldName().equals("title")) {
							title = Streams.asString(stream);
						}
						if (item.getFieldName().equals("author")) {
							author = Streams.asString(stream);
						}
						if (item.getFieldName().equals("description")) {
							description = Streams.asString(stream);
						}
						if (item.getFieldName().equals("genre")) {
							genre = Streams.asString(stream);
						}
					} else {
						// Handle the uploaded file
						if (item.getFieldName().equals("ebook")) {
							bEbook = new Blob(IOUtils.toByteArray(stream));
						}
						Date date = new Date();

						Ebook ebook = new Ebook(title, author, genre,
								description, date, bEbook);

						EbookDao.getInstance().insertEbook(ebook);
						resp.sendRedirect("index");
					}
				}
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
		}
		// edit and update image property
		if (method.equals("edit")) {
			Long id = null;
			Blob bImage = null;
			ServletFileUpload upload = new ServletFileUpload();
			FileItemIterator iterator = null;
			try {
				iterator = upload.getItemIterator(req);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			try {
				while (iterator.hasNext()) {
					FileItemStream item = iterator.next();
					InputStream stream = item.openStream();
					if (item.isFormField()) {
						if (item.getFieldName().equals("id")) {
							id = Long.parseLong(Streams.asString(stream));
						}
					} else {
						// Handle the uploaded file
						bImage = new Blob(IOUtils.toByteArray(stream));
						
						Ebook ebook = new Ebook(id, bImage);

						EbookDao.getInstance().update(ebook);
						resp.sendRedirect("index");
					}
				}
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
		}
		
	}
}
